#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Delete Images                                                      #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 10/01/2007                                             #
# Last Modification: 10/01/2007                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 45
#
# MANUAL: This script moves all choosen directories into a TRASH subdirectory. Use with extreme caution !!!
# MANUAL: This can now directly be done via the Library Tab in GUI
#
# DISPLAY: delete_in_out_trash
# DISPLAY: delete_sure
# DISPLAY: delete_trash
#
#$end_local_vars
#
# This defines some variables that we might need:
set proc_2dx = ""
set bin_2dx = ""
set app_2dx_image = ""
set FFTIR_DISK = ""
set delete_sure = ""
set delete_trash = ""
set delete_in_out_trash = ""
#
#$end_vars
#
# This sets the scriptname variable:
set scriptname = 2dx_deleteDir
#
# This removes the old logfile, so that a new fresh one can be created:
\rm -f LOGS/${scriptname}.results
#
# This initializes the CCP4 environment, and other variables.
set ccp4_setup = 'n'
source ${proc_2dx}/initialize
#
echo delete_in_out_trash = ${delete_in_out_trash}
echo delete_sure = ${delete_sure}
echo delete_trash = ${delete_trash}
#
echo "set delete_in_out_trash = 0" >> LOGS/${scriptname}.results
echo "set delete_sure = no" >> LOGS/${scriptname}.results
#
# This advances the progress bar to the 1% position:
echo "<<@progress: 1>>"
#
if ( ${delete_in_out_trash} == "0" ) then
  ${proc_2dx}/protest "Not doing anything."
endif
if ( ${delete_sure} == "no" ) then
  ${proc_2dx}/protest "Not running (Are you sure is 'no')."
endif
#
# This creates all needed subdirectories in the current merge directory, in case they are missing:
source ${proc_2dx}/2dx_makedirs 
#
# This memorizes the current merge directory under the variable "olddir":
set olddir = $PWD
cd ..
set rootdir = $PWD
#

if ( ${delete_in_out_trash} == "1" ) then
  if ( ${delete_sure} == "yes" ) then
        cd ${olddir}
	# This translates the list of directories to work on into one single long line:
	cat 2dx_merge_dirfile.dat | tr "\n" " " > SCRATCH/2dx_merge_dirfile_oneline.dat
	set dirlist = "`cat SCRATCH/2dx_merge_dirfile_oneline.dat`"
	#
        cd ${rootdir}
	if ( ! -d TRASH ) then
	  \mkdir TRASH
	endif
	cd TRASH
	if ( ! -l 2dx_master.cfg ) then
	  \ln -s ../2dx_master.cfg .
	endif
	cd ..
	#
	echo ":: "
	#
	# This initiates a big "foreach" loop that sets the variable "dirfile" to each of the items in the "dirlist" and runs the contents of the loop with this value:
	foreach dirfile ( ${dirlist} ) 
	  echo "::Moving into TRASH: ${dirfile}"
	  #
          cd ${rootdir}
	  \mv -f ${dirfile} TRASH 
	  #
	end
	#
	echo ":: "
	echo "::Done."
	echo "::Images are now in ${PWD}/TRASH "
	echo ":: "
  else
	${proc_2dx}/linblock "Not moving selected directories to TRASH  (Are you sure is 'no')."
  endif
endif
#
#
if ( ${delete_in_out_trash} == "2" ) then
  if ( ${delete_sure} == "yes" ) then
        cd ${olddir}
	# This translates the list of directories to work on into one single long line:
	cat 2dx_merge_dirfile.dat | tr "\n" " " > SCRATCH/2dx_merge_dirfile_oneline.dat
	set dirlist = "`cat SCRATCH/2dx_merge_dirfile_oneline.dat`"
	#
        cd ${rootdir}
	if ( ! -d UNTRASH ) then
	  \mkdir UNTRASH
	endif
	cd UNTRASH
	if ( ! -l 2dx_master.cfg ) then
	  \ln -s ../2dx_master.cfg .
	endif
	cd ..
	#
	echo ":: "
	#
	# This initiates a big "foreach" loop that sets the variable "dirfile" to each of the items in the "dirlist" and runs the contents of the loop with this value:
	foreach dirfile ( ${dirlist} ) 
	  echo "::Moving out of TRASH into UNTRASH: ${dirfile}"
	  #
          cd ${rootdir}
          \mv -f ${dirfile} UNTRASH 
	end
	#
	echo ":: "
	echo "::Done."
	echo "::Images are now in ${PWD}/UNTRASH "
	echo ":: "
  else
	${proc_2dx}/linblock "Not moving selected directories out of TRASH and into UNTRASH  (Are you sure is 'no')."
  endif
endif
#
#
if ( ${delete_in_out_trash} != "0" ) then
  if ( ${delete_trash} == "yes" ) then
	if ( -d TRASH ) then
                cd ${rootdir}
		${proc_2dx}/linblock "emptying TRASH"
		echo "<<@progress: 50>>"
                cd TRASH
		rm -rf *
                cd ..
		echo ":: "
		echo ":: Done."
		echo ":: TRASH is now empty"
		echo ":: "
	endif
  endif
endif
#
#
# Move the progress bar to 100%:
echo "<<@progress: 100>>"
#
# Leave a message on the logfile that the script finished:
${proc_2dx}/linblock "${scriptname} finished correctly."
#
echo "<<@evaluate>>"
#
